package com.competition.jingdong;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.util.LinkedList;

/**
 * @author henrydai
 * @date 2021/5/10 16:26
 */
public class WriteToCsv {
    static String title = "问题,用户名,订单号,包裹号,耗材名称,SKU,xStart,yStart,zStart,xEnd,yEnd,zEnd";
    static String title2 = "用户名,耗材名称,长,宽,高";
    static String charsetName = "UTF-8";
    Path file;
    WriteToCsv(Path file)
    {
        this.file=file;
    }
    //清空文件
    public void clear(){
        if(!Files.exists(file))
        {
            try {
                Files.createFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            BufferedWriter bw = Files.newBufferedWriter(file,Charset.forName(charsetName));
            bw.write(title);
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //表二清理
    public void clear2()
    {
        if(!Files.exists(file))
        {
            try {
                Files.createFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            BufferedWriter bw = Files.newBufferedWriter(file,Charset.forName(charsetName));
            bw.write(title2);
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //追加写
    public void write(LinkedList<OrderData> orderDataLinkedList)
    {
        try {
            BufferedWriter bw = Files.newBufferedWriter(file, Charset.forName(charsetName),StandardOpenOption.APPEND);
            for(OrderData od : orderDataLinkedList)
            {
                bw.write(od.toString());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //写入表二
    public void write2(LinkedList<NewConsume> newConsumeLinkedList)
    {
        try {
            BufferedWriter bw = Files.newBufferedWriter(file, Charset.forName(charsetName),StandardOpenOption.APPEND);
            for(NewConsume nc : newConsumeLinkedList)
            {
                bw.write(nc.toString());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
